package net.qujoy.java8.methodparam.second;

import net.qujoy.java8.domain.Apple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AppleClient {

    public static List<Apple> filter(List<Apple> params, ApplePredicate applePredicate) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : params) {
            if (applePredicate.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        List<Apple> params = Arrays.asList(new Apple("green", 501),
                new Apple("red", 400), new Apple("green", 300));

        List<Apple> result1 = filter(params, new AppleGreenColorPredicate());
        List<Apple> result2 = filter(params, new AppleHeavyWeightPredicate());

        //这样做会产生很多的只用一次的类，然后可以使用匿名内部类

        List<Apple> result3 = filter(params, new ApplePredicate() {
            @Override
            public boolean test(Apple apple) {
                return false;
            }
        });

        //匿名内部类略显罗嗦


        //引入使用lambda表达式

        List<Apple> result4 = filter(params, (Apple apple) -> {
            return apple.getWeight() > 500;
        });

        List<Apple> result5 = filter(params, (Apple apple) -> apple.getWeight() > 500);

        List<Apple> result6 = filter(params, apple -> apple.getWeight() > 500);

    }

}
